<?php
/**
 * Created by PhpStorm.
 * User: denh
 * Date: 16/6/7
 * Time: 上午11:32
 */
require_once ROOT_PATH . 'bs/BsUserManage.php';

class ApiController extends BaseController {

    function userInfo() {
        $phone = $this->request('phone', '', true);
        $doUser = new Do用户();
        $do医师 = new Do医师详情();
        $doUser->set_telephone($phone);
        if ($doUser->where($doUser->data)->row()) {
            $do医师->set_用户id($doUser->get_id());
            $do医师->where($do医师->data)->row();
            $data['is_subscribe'] = $doUser->get_unsubscribe();
            $data['name'] = $doUser->get_name();
            $data['is_auth'] = $doUser->get_认证() > 1 ? '1' : '0';
            $data['phone'] = $doUser->get_telephone();
            $data['openID'] = $doUser->get_openID();

            $data['hospital'] = $do医师->get_医院();
            $data['keshi'] = $do医师->get_专业领域();
            $data['zhicheng'] = $do医师->get_职务();
            $data['zigezhenghao'] = $do医师->get_资格证号();

            $avatar = $doUser->get_头像();
            if (!$avatar) {
                $avatar = G_SITE_BASE .'/business/img/default_avatar.jpg';
            } elseif (substr($avatar, 0, 4) != 'http') {
                $avatar = G_SITE_BASE .'/business/'. $avatar;
            }
            $data['avatar'] = $avatar;
            $this->jsonOutPut(0, '', $data);
        } else {
            $this->jsonOutPut(-1, '没有这个用户');
        }

    }

    #用户中心审核
    function userCenter_CheckUser() {
        $infos = $this->request('infos', '', true);
        $authResult = $this->request('authResult', '', true); #审核结果 0:未通过 1:通过
        $reason = $this->request('reason', '');

        $status = '';
        if ($authResult == '0') {
            $status = '未通过';
        } elseif ($authResult == '1') {
            $status = '通过';
        } else {
            $this->jsonOutPut(-1, '未定义的状态');
        }

        $infos = json_decode($infos);
        if (isset($infos->account->openId) and $infos->account->openId) {
            $openId = $infos->account->openId;

            $doUser = new Do用户();
            $doDoctor = new Do医师详情();
            $userId = $doUser->fields("id")->where("openID = '$openId'")->getOneField();
            $doctorId = $doDoctor->fields("id")->where("用户id = '$userId'")->getOneField();
            $re = BsUserManage::checkUser($doctorId, $status, $reason);
            if ($re) {
                $this->jsonOutPut(0, 'ok');
            } else {
                $this->jsonOutPut(-1, '审核失败');
            }
        } else {
            $this->jsonOutPut(-1, '参数infos格式错误');
        }
    }

    #用户中心修改用户资料
    function userCenter_modifyUserInfo() {
        $infos = $this->request('infos', '', true);
        $infos = json_decode($infos);

        $check = isset($infos->author->name)
                and isset($infos->author->icon)
                and isset($infos->author->status)
                and isset($infos->author->sector)
                and isset($infos->author->title)
                and isset($infos->author->hospital)
                and isset($infos->account->openId)
                and $infos->account->openId;
        if (!$check) {
            $this->jsonOutPut(-1, '参数infos格式错误');
        }

        $openId = $infos->account->openId;
        $data = array(
            'name' => $infos->author->name,
            'avatar' => $infos->author->icon,
            'status' => $infos->author->status,
            'keshi' => $infos->author->sector,
            'zhicheng' => $infos->author->title,
            'hospital' => $infos->author->hospital
        );
        $re = BsUserManage::modifyUserInfo($openId, 'openid', $data);
        if ($re) {
            $this->jsonOutPut(0, 'ok');
        } else {
            $this->jsonOutPut(-1, '修改失败');
        }
    }

    function wsJsParam() {
        $callback = $this->request('callback', '', true);
        $this->load_jssdk($_SERVER['HTTP_REFERER']);
        $this->signPackage['referer'] = $_SERVER['HTTP_REFERER'];
        $data = json_encode($this->signPackage);
        $data = sprintf('%s(%s)', $callback, $data);
        echo $data;
    }

    /**
     * 修改用户openId
     */
    function deleteUser() {
        if (!$this->checkSign()) {
            $this->jsonOutPut(-1, '签名不正确');
        }
        $uid = $this->request('uid', '', true);
        $do = new Do用户();
        $do->set_userCenterID($uid);
        if ($do->where($do->data)->row()) {
            $do->set_openID($do->get_openID(). '|'. time());
            if ($do->update()) {
                $this->jsonOutPut(0, 'ok');
            }
        }
        $this->jsonOutPut(-1, 'error');
    }

    /**
     * 批量修改用户信息
     */
    function batchModifyUserInfo() {
        $type = $this->request('type', '', true);
        $old = $this->request('old', '', true);
        $new = $this->request('new', '', true);
        $re = BsUserManage::batchModifyUserInfo($type, $old, $new);

        $this->jsonOutPut(0, 'ok', $re);
    }

    /**
     * 判断接口签名
     * @return bool
     */
    private function checkSign() {
        $sign = $this->request('sign', '', true);

        $request = $_REQUEST;
        unset($request['sign']);
        ksort($request);
        foreach($request as $k => $v) {
            $request[$k] = $k . '=' . $v;
        }
        $sign_check = md5( implode('#', $request) . '#' . self::SECRET_KEY );

        if ($sign != $sign_check) {
            return false;
        }
        return true;
    }

    function uploadToQiniu() {
        $fileName = $this->request('fileName','', true);
        $imgUrl = Tools::upload_qiniu_pic($fileName);
        if($imgUrl){
            $this->jsonOutPut(0, '图片上传成功', $imgUrl);
        }else{
            $this->jsonOutPut(-1, '图片上传失败');
        }
    }

    function test() {
        $callback = $this->request('bb', '', true);
        $arr = array(
            'name' => 'hello',
            'sex'  => '1',
            'age'  => '50'
        );
        $data = json_encode($arr);
        $data = sprintf('%s(%s)', $callback, $data);
        echo $data;
    }

    function inset_doctor() {
        return;
        error_reporting(0);
        require ROOT_PATH . 'tools/import_excel/reader.php';

        $data = new Spreadsheet_Excel_Reader(); // Set output Encoding.
        $data->setOutputEncoding('UTF-8');
        $data->read('test/doctor_list_1.xls');

       # print_r($data->sheets[0]['cells']);die;


        foreach ($data->sheets[0]['cells'] as $k => $v) {
            $do = new DoDoctor_auto_pass();
            $do->set_phone($v['1']);
            if ($do->where($do->data)->count()) {
                continue;
            }
            $do->set_name($v['3']);
            #$do->set_hospital($v['3']);
            #$do->set_keshi($v['4']);
            #$do->set_zhicheng($v['5']);
            $do->save();
        }

    }
}